from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QTableWidgetItem, QApplication
from PyQt5.QtCore import QTranslator
from classes.engine import Engine
from ui_py.page6_ui import Main_Ui
from temp import Excel, ReadExcel
from expert import Expert
from grap import *
from validation import validation
from error import Error
import json
from dataExpert import Data
class Page6(QtWidgets.QWidget, Main_Ui):
    engine = Engine()
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.setupUi(self)
        with open('lang.json', encoding='utf-8') as file:
            data = json.load(file)
        if data['lang'] != "":
            self.trans = QTranslator(self)
            self.trans.load('translate/' + 'page6')
            _app = QApplication.instance() # получить экземпляр приложения
            _app.installTranslator(self.trans)
        self.expert = Expert()
        self.err = Error()
        self.data = Data()
        self.err.clear = 0
    #     self.comboBox.setPlaceholderText("Выберите")
    #     self.comboBox.addItem("Приведенные к радиусам кривошипа", 1)
    #     self.comboBox.addItem("Неуравновешенные силы", 2)
    #     self.comboBox.currentIndexChanged.connect(self.handleActivated)
    #     # self.tableWidget.cellChanged.connect(self.cellChanged)
                    
    # ПЕРВАЯ СТРОКА
                    
        # row = self.tableWidget.rowCount()
        # self.tableWidget.insertRow(row)

        # row = self.tableWidget.rowCount()
        # self.tableWidget.insertRow(row)
        # self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(""))

        # self.tableWidget.setItem(row, 0, QTableWidgetItem("Сила, Н"))
        # self.tableWidget.setItem(row, 1, QTableWidgetItem("Угол, град"))
        # self.tableWidget.setItem(row, 2, QTableWidgetItem("Плечо, мм"))

    # ВТОРАЯ СТРОКА
        
        row = self.tableWidget.rowCount()
        self.tableWidget.insertRow(row)
        if data['lang'] != "":
            page = data['en']['page6']
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][0]))
        else:
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Внешняя сила 1"))

        try:
            value1 = str(self.data.excel['Extforce1'])
        except:
            value1 = ""

        try:
            value2 = str(self.data.excel['Dirforce1'])
        except:
            value2 = ""

        try:
            value3 = str(self.data.excel['Armforce1'])
        except:
            value3 = ""
        self.tableWidget.setItem(row, 0, QTableWidgetItem(value1))
        self.tableWidget.setItem(row, 1, QTableWidgetItem(value2))
        self.tableWidget.setItem(row, 2, QTableWidgetItem(value3))

    # ТРЕТЬЯ СТРОКА
        
        row = self.tableWidget.rowCount()
        self.tableWidget.insertRow(row)
        if data['lang'] != "":
            page = data['en']['page6']
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][1]))
        else:
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Внешняя сила 2"))

        try:
            value1 = str(self.data.excel['Extforce2'])
        except:
            value1 = ""

        try:
            value2 = str(self.data.excel['Dirforce2'])
        except:
            value2 = ""
        try:
            value3 = str(self.data.excel['Armforce2'])
        except:
            value3 = ""

        self.tableWidget.setItem(row, 0, QTableWidgetItem(value1))
        self.tableWidget.setItem(row, 1, QTableWidgetItem(value2))
        self.tableWidget.setItem(row, 2, QTableWidgetItem(value3))

    # ЧЕТВЁРТАЯ СТРОКА
        
        row = self.tableWidget.rowCount()
        self.tableWidget.insertRow(row)
        if data['lang'] != "":
            page = data['en']['page6']
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][2]))
        else:
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Внешняя сила 3"))

        try:
            value1 = str(self.data.excel['Extforce3'])
        except:
            value1 = ""

        try:
            value2 = str(self.data.excel['Dirforce3'])
        except:
            value2 = ""

        try:
            value3 = str(self.data.excel['Armforce3'])
        except:
            value3 = ""

        self.tableWidget.setItem(row, 0, QTableWidgetItem(value1))
        self.tableWidget.setItem(row, 1, QTableWidgetItem(value2))
        self.tableWidget.setItem(row, 2, QTableWidgetItem(value3))

    # ПЯТАЯ СТРОКА

        row = self.tableWidget.rowCount()
        self.tableWidget.insertRow(row)
        if data['lang'] != "":
            page = data['en']['page6']
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][3]))
        else:
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Внешняя сила 4"))

        try:
            value1 = str(self.data.excel['Extforce4'])
        except:
            value1 = ""

        try:
            value2 = str(self.data.excel['Dirforce4'])
        except:
            value2 = ""

        try:
            value3 = str(self.data.excel['Armforce4'])
        except:
            value3 = ""

        self.tableWidget.setItem(row, 0, QTableWidgetItem(value1))
        self.tableWidget.setItem(row, 1, QTableWidgetItem(value2))
        self.tableWidget.setItem(row, 2, QTableWidgetItem(value3))

    # ШЕСТАЯ СТРОКА

        row = self.tableWidget.rowCount()
        self.tableWidget.insertRow(row)
        if data['lang'] != "":
            page = data['en']['page6']
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem(page['table'][4]))
        else:
            self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Внешняя сила 5"))

        try:
            value1 = str(self.data.excel['Extforce5'])
        except:
            value1 = ""

        try:
            value2 = str(self.data.excel['Dirforce5'])
        except:
            value2 = ""

        try:
            value3 = str(self.data.excel['Armforce5'])
        except:
            value3 = ""

        self.tableWidget.setItem(row, 0, QTableWidgetItem(value1))
        self.tableWidget.setItem(row, 1, QTableWidgetItem(value2))
        self.tableWidget.setItem(row, 2, QTableWidgetItem(value3))
        self.tableWidget.cellChanged.connect(self.cellChange)

    def cellChange(self, row, column):
        if validation(self.tableWidget.item(row, column).text(), 'Armforce1', self.data.excel):
            # excel = ReadExcel(self.filename)
            for i in range(0, 5):
                if column == 0:
                    if row == i:
                        self.expert.excel['Extforce{}'.format(i+1)] = self.tableWidget.item(row, column).text()
                        self.data.excel['Extforce{}'.format(i+1)] = self.tableWidget.item(row, column).text()
                        # excel.updateCell('Extforce{}'.format(i+1), self.tableWidget.item(row, column).text(), self.filename)
                        self.tableWidget.blockSignals(True)
                        self.tableWidget.item(row, column).setBackground(QtGui.QColor(112, 255, 127))
                        self.tableWidget.blockSignals(False)
                        break
                elif column == 1:
                    if row == i:
                        self.expert.excel['Dirforce{}'.format(i+1)] = self.tableWidget.item(row, column).text()
                        self.data.excel['Dirforce{}'.format(i+1)] = self.tableWidget.item(row, column).text()
                        # excel.updateCell('Dirforce{}'.format(i+1), self.tableWidget.item(row, column).text(), self.filename)
                        self.tableWidget.blockSignals(True)
                        self.tableWidget.item(row, column).setBackground(QtGui.QColor(112, 255, 127))
                        self.tableWidget.blockSignals(False)
                        break
                elif column == 2:
                    if row == i:
                        self.expert.excel['Armforce{}'.format(i+1)] = self.tableWidget.item(row, column).text()
                        self.data.excel['Armforce{}'.format(i+1)] = self.tableWidget.item(row, column).text()
                        # excel.updateCell('Armforce{}'.format(i+1), self.tableWidget.item(row, column).text(), self.filename)
                        self.tableWidget.blockSignals(True)
                        self.tableWidget.item(row, column).setBackground(QtGui.QColor(112, 255, 127))
                        self.tableWidget.blockSignals(False)
                        break
            try:
                if self.err.error_dict[key] > 0:
                    del self.err.error_dict[key]
            except:
                pass
        else:
            if column == 0:
                key = f"Extforce{row+1}"
            elif column == 1:
                key = f"Dirforce{row+1}"
            elif column == 2:
                key = f"Armforce{row+1}"
            self.tableWidget.blockSignals(True)
            self.tableWidget.item(row, column).setBackground(QtGui.QColor(255, 152, 152))
            self.tableWidget.blockSignals(False)
            self.err.error_dict[key] = 6
            self.err.page = 6